I have xml files which contain a <!DOCTYPE> with a link to a dtd. On my
local computer this this link is invalid as it refers to a folder that
does not exist. The link has the format "file://localhost...".
Using an System.Xml.XmlDocument object and calling the Load() function,
the parses hangs for about 5 minutes and then thows an Exception with
the text "the semaphore timeout period has expired".
As the MSDN states the XmlDocument.Load() method does not do dtd
validation so I wonder what is happening. Does the parser try to load
the dtd file and gets a network timeout?
Are there any switches in XmlDocument that I can use to ignore the dtd?
Thanks,
Chris
Chris wrote:
> Are there any switches in XmlDocument that I can use to ignore the dtd?
You would need to assign an XmlResolver that returns null for any URL
passed in e.g. (pseudo code)
class DummyResolver : XmlUrlResolver {
public override Uri ResolveUri (Uri baseUri, String relativeUri) {
return null;
}
}
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.XmlResolver = new DummyResolver();
xmlDocument.Load(@"whatever.xml");
But you need to be aware that a DTD is not only necessary for validation
but can also be needed to resolve entity references.
And the behavior of XmlDocument and its XmlResolver depends on different
factors, see
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXmlDocumentClassXmlResolverTopic.asp>
--
Martin Honnen
http://JavaScript.FAQTs.com/
Instead of returing 'null' from DummyResolver.ResolveUri(), try returning an
empty stream from DummyResolver.GetEntity() whenever you get a
resolution request for any uri that you don't want resolved. This should
solve your problem.
Ion
"Martin Honnen" <maho...@yahoo.de> wrote in message
news:%23v98ucw...@TK2MSFTNGP14.phx.gbl...
>
>
> Chris wrote:
>
>
> > using your suggested method seems the right way.
> > Doing so, I get an Exception "object reference not set to an instance of
> > an object" as soon as I 'return null' in the overridden method.
> > The ResolveUri function is also often called with the document parsed,
> > but with an empty baseUri. So I think that the baseUri is only != null
> > when the parser tries to resolve a link. So I tried:
> >
> > if( baseUri != null )
> > return null;
> > else
> > return base.ResolveUri( baseUri, relativeUri );
> >
> > But as soon as I return null I get the above mentioned error.
> > Any suggestions?
>
> I am not sure how to solve that, looking at the documentation I had
> hoped my suggestion would work out but it seems it doesn't, the
> documentation here
>
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html
/frlrfsystemxmlxmlresolverclassresolveuritopic.asp>
> explicitly says: "Return Value
> A Uri representing the absolute URI or a null reference (Nothing in
> Visual Basic) if the relative URI can not be resolved."
> Therefore I hoped if the resolver returns null the code calling it would
> not further try to deal with the URI and simply ignore it and not try to
> load it but that is not the case it seems.
Instead of returning 'null' from ResolveUri(), return an empty stream
from GetEntity() whenever you get a resolution request for the known
'bad' uris. Regards,
Ion
"Chris" <chris...@hotmail.com> wrote in message
news:OJDI$1vLFH...@TK2MSFTNGP15.phx.gbl...
> Thanks for your reply,
> using your suggested method seems the right way.
> Doing so, I get an Exception "object reference not set to an instance of
> an object" as soon as I 'return null' in the overridden method.
> The ResolveUri function is also often called with the document parsed,
> but with an empty baseUri. So I think that the baseUri is only != null
> when the parser tries to resolve a link. So I tried:
>
> if( baseUri != null )
> return null;
> else
> return base.ResolveUri( baseUri, relativeUri );
>
> But as soon as I return null I get the above mentioned error.
> Any suggestions?
> Thanks,
> Chris
if( baseUri != null )
return null;
else
return base.ResolveUri( baseUri, relativeUri );
But as soon as I return null I get the above mentioned error.
Any suggestions?
Thanks,
Chris
Chris wrote:
> using your suggested method seems the right way.
> Doing so, I get an Exception "object reference not set to an instance of
> an object" as soon as I 'return null' in the overridden method.
> The ResolveUri function is also often called with the document parsed,
> but with an empty baseUri. So I think that the baseUri is only != null
> when the parser tries to resolve a link. So I tried:
>
> if( baseUri != null )
> return null;
> else
> return base.ResolveUri( baseUri, relativeUri );
>
> But as soon as I return null I get the above mentioned error.
> Any suggestions?
I am not sure how to solve that, looking at the documentation I had
hoped my suggestion would work out but it seems it doesn't, the
documentation here
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlxmlresolverclassresolveuritopic.asp>
explicitly says: "Return Value
A Uri representing the absolute URI or a null reference (Nothing in
Visual Basic) if the relative URI can not be resolved."
Therefore I hoped if the resolver returns null the code calling it would
not further try to deal with the URI and simply ignore it and not try to
load it but that is not the case it seems.
Thanks,
Chris
Here's the code that should do the trick:
class DummyResolver : XmlUrlResolver {
...
public override object GetEntity(Uri absoluteUri, string role, Type
ofObjectToReturn) {
if (absoluteUri != null && absoluteUri.AbsoluteUri == "<whatever you want
to override>") {
if (ofObjectToReturn == null || ofObjectToReturn ==
typeof(System.IO.Stream)) {
return Stream.Null;
}
}
return base.GetEntity(absoluteUri, role, ofObjectToReturn);
}
Regards,
Ion
"Chris" <chris...@hotmail.com> wrote in message
news:OsDZR4$LFHA...@TK2MSFTNGP10.phx.gbl...
Regads,